//
//  TGTopMenu.m
//  GroupBuy
//
//  Created by wwj on 15/11/4.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGTopMenu.h"
#import "TGCategoryMenu.h"
#import "TGDistrictMenu.h"
#import "TGTopMenuItem.h"
#import "TGOrderMenu.h"
#import "TGOrderModel.h"
#import "TGDataTool.h"

@interface TGTopMenu ()
@property (weak ,nonatomic) TGTopMenuItem *menuItem;

@property (strong ,nonatomic) TGCategoryMenu *cMenu;
@property (strong ,nonatomic) TGDistrictMenu *dMenu;
@property (strong ,nonatomic) TGOrderMenu *oMenu;
@property (strong ,nonatomic) TGBottomMenu *bMenu;

@property (strong, nonatomic) TGTopMenuItem *cTMeu;
@property (strong, nonatomic) TGTopMenuItem *dTMeu;
@property (strong, nonatomic) TGTopMenuItem *oTMeu;
@end

@implementation TGTopMenu
+(instancetype)topMenu{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        kAddNotification(dataChange)
        
    }
    return self;
}

-(void)dataChange{
    NSString *cStr = [TGDataTool shareDataTool].currentCategory;
    if (cStr) {
        _cTMeu.title = cStr;
    }
    
    NSString *dStr = [TGDataTool shareDataTool].currentDistrict;
    if (dStr) {
        _dTMeu.title = dStr;
    }
    
    NSString *oStr = [TGDataTool shareDataTool].currentOrder.name;
    if (oStr) {
        _oTMeu.title = oStr;
    }
    
    [_bMenu hidden];
}

- (void)initView{
    // 1.全部分类
    _cTMeu = [self addMenuItem:@"全部分类" index:0];
    
    // 2.全部商区
    _dTMeu = [self addMenuItem:@"全部商区" index:1];
    
    // 3.默认排序
    _oTMeu = [self addMenuItem:@"默认排序" index:2];
}

- (TGTopMenuItem *)addMenuItem:(NSString *)title index:(int)index{
    TGTopMenuItem *menuItem = [[TGTopMenuItem alloc] init];
    menuItem.tag = index;
    menuItem.title = title;
    menuItem.frame =CGRectMake(index * kTopMenuItemW, 0, 0, 0);
    [self addSubview:menuItem];
    
    [menuItem addTarget:self action:@selector(onClickMenuItem:) forControlEvents:UIControlEventTouchUpInside];
    
    return menuItem;
}

- (void)onClickMenuItem:(TGTopMenuItem *)itemBtn{
    
    if ([TGDataTool shareDataTool].currentCity.name == nil) {
        [MBProgressHUD showError:@"请选择城市"];
        return;
    }
    
    _menuItem.selected = NO;
    if (_menuItem == itemBtn) {
        _menuItem = nil;
        //隐藏菜单
        [self hiddenMenu:itemBtn];
    }else{
        itemBtn.selected = YES;
        _menuItem =itemBtn;
        //显示菜单
        [self showMenu:itemBtn];
    }
}

#pragma -mark 隐藏菜单
-(void)hiddenMenu:(TGTopMenuItem *)btn{
    [_bMenu hidden];
    _bMenu = nil;
}

#pragma -mark 显示菜单
-(void)showMenu:(TGTopMenuItem *)btn{
    //判断是否第一次创建底部菜单
    BOOL animation = (_bMenu == nil);
     [_bMenu removeFromSuperview];
    switch (btn.tag) {
        case 0://显示分类
        {
            if (_cMenu == nil) {
                _cMenu = [[TGCategoryMenu alloc] init];
            }
            _bMenu = _cMenu;
        }
            break;
            
        case 1://显示区域
        {
            if (_dMenu == nil) {
                _dMenu = [[TGDistrictMenu alloc] init];
            }
            _bMenu = _dMenu;
        }
            break;
            
        case 2://显示排序
        {
            if (_oMenu == nil) {
                _oMenu = [[TGOrderMenu alloc] init];
            }
            _bMenu = _oMenu;
        }
            break;
    }
     _bMenu.frame = _contentView.bounds;
    [_contentView addSubview:_bMenu];
    
    __block TGTopMenu *topMenu =self;
    _bMenu.hiddenBlock = ^{
        topMenu.menuItem.selected = NO;
        topMenu.menuItem = nil;
        topMenu.bMenu = nil;
    };
    if (animation) {//第一次创建底部菜单的时候执行显示动画
        [_bMenu show];
    }
}

-(void)setContentView:(UIView *)contentView{
    _contentView = contentView;
}

-(void)setFrame:(CGRect)frame{
    frame.size = CGSizeMake(kTopMenuItemW * 3, kTopMenuItemH);
    [super setFrame:frame];
}
@end
